package com.example.myskyjava.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.myskyjava.ThrowData.Result;
import com.example.myskyjava.domain.SkyData;
import com.example.myskyjava.mapper.SkyMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


@Service
public class SkyService  extends ServiceImpl<SkyMapper, SkyData> {

    @Resource
    private SkyMapper SkyMapper;

    public Result add(SkyData SkyData){
        Result result = new Result();
        boolean save = this.save(SkyData);
//        final LambdaQueryWrapper<com.example.myskyjava.domain.SkyData> eq = new LambdaQueryWrapper<SkyData>().eq(com.example.myskyjava.domain.SkyData::getAge, 14);
//
//        this.list(eq);

//        SkyMapper.insert(SkyData);
//        boolean save (SkyData);
        if(save){
            result.setMsg("请求成功");
            result.setSuccess(true);
        }else {
            result.setMsg("请求失败");
            result.setSuccess(false);
        }

        return result;

    }


    public Result select(SkyData skyData){
        Result result = new Result();
        List<SkyData> list1 = this.lambdaQuery()
                .eq(Objects.nonNull(skyData.getId()), SkyData::getId, skyData.getId())
                .eq(skyData.getName() != null && !skyData.getName().equals(""), SkyData::getName, skyData.getName()).list();
        List<SkyData> list = list(this.setData(skyData));
        result.setData(list);
        result.setSuccess(true);

        return result;
    }

    public Result deletMsg(SkyData skyData){
        Result result = new Result();
        QueryWrapper<SkyData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(skyData.getId()),"id",skyData.getId());
        boolean remove = remove(queryWrapper);
        if(remove){
            result.setData("");
            result.setMsg("删除成功");
            result.setSuccess(true);
        }

        return result;
    }

    public Result updateMsg(SkyData skyData){
        Result result = new Result();
        if(skyData.getId() == null){
            result.setMsg("id不能为空");
            result.setSuccess(false);
            return result;
        }
        UpdateWrapper<SkyData> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq(Objects.nonNull(skyData.getId()),"id",skyData.getId());
        boolean update = update(skyData,updateWrapper);
        if(update){
            result.setData("");
            result.setMsg("编辑成功");
            result.setSuccess(true);
        }
        return result;
    }

    public QueryWrapper setData(SkyData skyData){
        QueryWrapper<SkyData> queryWrapperSdf = new QueryWrapper<>();
        queryWrapperSdf.eq(Objects.nonNull(skyData.getId()),"id",skyData.getId())
                .eq(skyData.getName()!=null && !skyData.getName().equals("") ,"name",skyData.getName())
                .eq(skyData.getSex()!=null && !skyData.getSex().equals("") ,"sex",skyData.getSex())
                .eq(Objects.nonNull(skyData.getAge()),"age",skyData.getAge())
                .eq(skyData.getPhone()!=null && !skyData.getPhone().equals("") ,"phone",skyData.getPhone())
                .eq( skyData.getHobby()!=null && !skyData.getHobby().equals(""),"hobby",skyData.getHobby())
                .eq(skyData.getQq()!=null && !skyData.getQq().equals(""),"qq",skyData.getQq());

        return queryWrapperSdf;

    }


}
